package src.main.java.org.stream;

import java.util.*;
import java.util.stream.Collectors;

/*
 * stream流的常用方法_终结方法
 * */
public class Test3 {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        Student s1 = new Student("蜘蛛精", 172, 26);
        Student s2 = new Student("蜘蛛精", 172, 26);
        Student s3 = new Student("张思", 171.4, 22);
        Student s4 = new Student("王伟", 172.2, 23);
        Student s5 = new Student("唐僧", 178.1, 24);
        Student s6 = new Student("孙悟空", 174, 25);
        Collections.addAll(students,s1,s2,s3,s4,s5,s6);
        //计算身高超过168的学生有几个人
        long count = students.stream().filter(s -> s.getHeight() > 168).count();
        System.out.println(count);
        //找出身高最高的学生对象
        Student student = students.stream().max((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
        System.out.println(student);
        //找出身高最矮的学生对象
        Student student1 = students.stream().min((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
        System.out.println(student1);
        //收集stream流
        //找出身高超过170学生对象，并放到一个新集合中返回
        //流只能收集一次
        List<Student> collect = students.stream().filter(s -> s.getHeight() > 170).collect(Collectors.toList());
        System.out.println(collect);
        //找出身高超过170学生对象，并放到一个set集合
        Set<Student> collect1= students.stream().filter(s -> s.getHeight() > 170).collect(Collectors.toSet());
        System.out.println(collect1);
        //找出身高超过170学生对象，并把学生的名字和身高，并放到一个Map集合
        Map<String, Double> collect2 = students.stream().
                filter(a -> a.getHeight() > 170)
                .distinct().
                collect(Collectors.toMap(a -> a.getName(), a -> a.getHeight()));
        System.out.println(collect2);
        //找出身高超过170学生对象，并放到一个数组
        Student[] objects = students.stream().filter(a -> a.getHeight() > 170).toArray(len->new Student[len]);


    }
}
